package com.cn.yugan.Utils.CommentListView;

import java.util.Map;

import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.view.View.OnClickListener;

import com.cn.yugan.Cons.FFConstants;
import com.cn.yugan.Engine.Beans.CommentFloor;
import com.cn.yugan.mobile.R;
import com.cn.yugan.Utils.MyViews.MyCommentBgView;


/**
 * @author chengc
 * @ClassName: CommentStyleUtils
 * @Description: 评论样式设置器
 * @date 2016-7-20 下午3:59:21
 */
public class CommentStyleUtils {
    /*
     * 可能的样式情况:
	 * 
	 * 1,最顶层-所有显示的顶部引用都是圆角.底部样式部分是直角---如果是第3层,那最后一层应该是圆角
	 * 
	 * 2,最底层-所有的显示都去掉.无背景
	 * 
	 * 3,情况太多,边写边看.我恨算法.
	 */

    /**
     * 根据评论所在楼层设置评论样式
     *
     * @param holder
     * @param num
     * @param sum
     * @author chengc
     */
    public static void setCommentStyleByNum(Context mContext,
                                            CommentViewHolder holder, CommentFloor curFloor,
                                            Map<CommentFloor, OnClickListener> hideBtnOnclickMap, int sum,
                                            int num, int flagHiden, int hideSum) {
        Resources resources = mContext.getResources();
        int padding = (int) resources
                .getDimension(R.dimen.my_view_line_padding);
        // 根据楼层号判断样式
        if (sum <= 2) {
            holder.styleView.setBgStyle(MyCommentBgView.MyBgStyle.END);
            holder.styleView.setTierNum(num - 1);
            holder.layoutCommentParent.setPadding(padding * (num - 1), padding
                    * (num - 1), padding * (num - 1), padding * (num - 1));
        } else if (sum <= 6) {
            holder.styleView.setTierNum(num - 1);
            holder.layoutCommentParent.setPadding(padding * (num - 1), padding,
                    padding * (num - 1), padding);
            if (num == sum) {
                holder.styleView.setBgStyle(MyCommentBgView.MyBgStyle.HEAD);
                holder.layoutCommentParent.setPadding(padding * (num - 1),
                        padding * (num - 1), padding * (num - 1), padding);
            } else if (num == 2) {
                holder.styleView.setBgStyle(MyCommentBgView.MyBgStyle.END);

            } else {
                holder.styleView
                        .setBgStyle(MyCommentBgView.MyBgStyle.CENTRE_END);
            }
        } else if (sum > 6) {
            int needPaddingNum = num - 1;
            if (num > 6) {
                needPaddingNum = 5;
            }
            holder.layoutCommentParent.setPadding(padding * needPaddingNum,
                    padding, padding * needPaddingNum, padding);
            if (num == 2) {
                holder.styleView.setTierNum(num - 1);
                holder.styleView.setBgStyle(MyCommentBgView.MyBgStyle.END);
            } else if (num <= 6) {
                holder.styleView.setTierNum(num - 1);
                holder.styleView
                        .setBgStyle(MyCommentBgView.MyBgStyle.CENTRE_END);
            } else if (num == sum) {
                holder.styleView.setTierNum(5);
                holder.styleView.setBgStyle(MyCommentBgView.MyBgStyle.MAX_HEAD);
                holder.layoutCommentParent.setPadding(padding * needPaddingNum,
                        padding * needPaddingNum, padding * needPaddingNum,
                        padding);
            } else {
                holder.styleView.setTierNum(5);
                holder.styleView
                        .setBgStyle(MyCommentBgView.MyBgStyle.MAX_CENTRE);
            }
        }

        // 判断-隐藏重复楼层按钮
        switch (flagHiden) {
            case FFConstants.CommentHideFlag.noNeedBtn:
                holder.layoutContent.setVisibility(View.VISIBLE);
                holder.layoutBtn.setVisibility(View.GONE);
                break;
            case FFConstants.CommentHideFlag.floorHide:
                holder.layoutContent.setVisibility(View.GONE);
                holder.layoutBtn.setVisibility(View.VISIBLE);
                holder.tvHideBtn.setText("重复引用" + hideSum + "楼已隐藏");
                setHideBtnOnclickListener(holder, curFloor, hideBtnOnclickMap);
                break;
            case FFConstants.CommentHideFlag.floorShow:
                holder.layoutContent.setVisibility(View.VISIBLE);
                holder.layoutBtn.setVisibility(View.VISIBLE);
                holder.tvHideBtn.setText("重复引用" + hideSum + "楼已显示");
                setHideBtnOnclickListener(holder, curFloor, hideBtnOnclickMap);
                break;
        }
    }

    /**
     * 设置对应楼层的隐藏/显示重复引用点击事件
     *
     * @param holder
     * @param curFloor
     * @author chengc
     */
    private static void setHideBtnOnclickListener(CommentViewHolder holder,
                                                  CommentFloor curFloor,
                                                  Map<CommentFloor, OnClickListener> hideBtnOnclickMap) {
        OnClickListener listener = null;
        listener = hideBtnOnclickMap.get(curFloor);
        holder.layoutBtn.setOnClickListener(listener);
    }
}
